﻿using Git.Storage.Common;
using Git.Storage.Entity.InStorage;
using Git.Storage.Provider.InStorage;
using Git.Storage.Provider.Move;
using Git.Storage.Provider.OutStorage;
using Git.Storage.WebApi.Common;
using Git.Storage.WebApi.Models.Input.Agv;
using Git.Storage.WebApi.Servers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace Git.Storage.WebApi.Controllers
{
    public class AgvController : ApiController
    {
        /// <summary>
        /// agv任务反馈
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult TaskFeedback(TaskFeedbackModel model)
        {
           /* var a = model.TaskType;
            var b = model.TypeCode;
            var c = model.Start;
            string zong=a+","+b+","+c;
            string filePath = "D:\\日志\\入库.txt";

            // 将数据写入txt文件
            using (StreamWriter writer = new StreamWriter(filePath, true))
            {
                writer.WriteLine(zong);
            }*/
            
            try
            {
               
                string dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string msg = "任务反馈成功";
                string code = "200";
                string ordernum = model.TypeCode;
                int taskType = model.TaskType;
                
                //验证单号有没有
                 

                // 处理异常情况
                var response = new
                {
                    code = code,
                    msg = msg,
                    dateTime = dateTime
                };
                // 根据业务逻辑进行任务反馈的处理
                if (string.IsNullOrEmpty(ordernum))
                {
                    code = "500";
                    msg = "任务单号不能为null";
                }
                if(model.Start == 1)
                {
                    // 处理异常情况
                    return Json(response);
                }
                //入库
                if (taskType == 0)
                {
                    InStorage instorage = new InStorage();
                    instorage.Proc_InStorage(ordernum);
                }
                //出库
                else if (taskType == 1)
                {
                    OutStorage outstorage = new OutStorage();
                    outstorage.Proc_InStorage(ordernum);
                }
                //移库
                else if(taskType == 2)
                {
                    MoveStorage movestorage = new MoveStorage();
                    movestorage.Proc_MoveStorage(ordernum);
                }
                else
                {
                    code = "500";
                    msg = "任务类型不对";
                }
                // 假设在此处对任务进行处理，并得到处理结果
                response = new
                {
                    code = code,
                    msg = msg,
                    dateTime = dateTime
                };

                return Json(response);
            }
            catch (Exception ex)
            {
                // 处理异常情况
                var response = new
                {
                    code = 500,
                    msg = "任务反馈失败：" + ex.Message,
                    dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                };
                return Json(response);
            }
        }
    }
}